package cn.zwx.member.svc.controller.address;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.member.svc.dto.AddressCreateReqDTO;
import cn.zwx.member.svc.dto.AddressQueryDTO;
import cn.zwx.member.svc.dto.AddressUpdateReqDTO;
import cn.zwx.member.svc.service.AddressService;
import cn.zwx.member.svc.vo.AddressRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author zhangwenxue
 */
@RestController
@Api(tags = "会员地址相关相关接口")
@RequestMapping("/address")
public class AddressController {

    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @PostMapping("/create")
    @ApiOperation("创建用户收件地址")
    public ResponseEntity<Long> createAddress(@Valid@RequestBody AddressCreateReqDTO dto) throws BizWrapException {
        return ResponseEntity.success(addressService.createAddress(dto));
    }

    @PostMapping("/update")
    @ApiOperation("更新用户收件地址")
    public ResponseEntity<Boolean> updateAddress(@Valid@RequestBody AddressUpdateReqDTO dto) throws BizWrapException {
        return ResponseEntity.success(addressService.updateAddress(dto));
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户收件地址")
    public ResponseEntity<Boolean> deleteAddress(@Valid@NotNull(message = "地址Id不能为空")@RequestParam Long id,
                                                 @Valid@NotNull(message = "用户Id不能为空") @RequestParam Long userId) throws BizWrapException {
        return ResponseEntity.success(addressService.deleteAddress(id,userId));
    }

    @GetMapping("/get")
    @ApiOperation("获得用户收件地址")
    public ResponseEntity<AddressRespVO> getAddress(@Valid AddressQueryDTO dto) {
        return ResponseEntity.success(addressService.getAddress(dto));
    }


    @GetMapping("/get-default")
    @ApiOperation("获得默认的用户收件地址")
    public ResponseEntity<AddressRespVO> getDefaultUserAddress(@Valid@RequestParam("userId") Long userId){
        return ResponseEntity.success(addressService.getDefaultUserAddress(userId));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户收件地址列表")
    public ResponseEntity<List<AddressRespVO>> getAddressList(@Valid@RequestParam("userId") Long userId){
        return ResponseEntity.success(addressService.getAddressList(userId));
    }
}
